{$APPTYPE console}{$O+}
uses commdlg,windows,messages,winspool,kol,shellapi,koladd{,kolprinters};{$I w32.pas}
var M:TMetafile;f1,f2:string;

const usage='Usage: EMF2PS file1.emf [file2.ps] ==) Convert emf/wmf file to ps file '+#13#10
	+'Note: You must install MTeX Ps-Printer. This program will output correct boundingbox for corresponding EPS.' ;

{$IFNDEF WIN32} 
const MAX_PATH = 144; 
{$ENDIF} 

var Form1,Button1:PControl;


//OpenPrinter

{
FillChar(ftagPD,sizeof(tagPD),0);
ftagPD.Flags := PD_RETURNDC + PD_RETURNDEFAULT;
ftagPD.lStructSize := sizeof(ftagPD);
hDevMode := ftagPD.hDevMode;
fDeviceMode  := PDevMode(GlobalLock(hDevMode));
Size := sizeof(DevMode^);
hDevMode := Printer.Info.ADevMode;//GlobalAlloc(GHND,Size);
DevMode := PDevMode(GlobalLock(hDevMode));

StrPCopy(DevMode^.dmDeviceName,'Ps-Printer');//Height;
DevMode^.dmPaperLength:=1000;//Height;
DevMode^.dmPaperWidth:=1000;//Width;
DevMode^.dmPaperSize := 0;
DevMode^.dmFields := DM_PAPERSIZE or DM_PAPERLENGTH or DM_PAPERWIDTH;
}


procedure Emf2Ps(emf,ps:string);
var 
	hemf:HENHMETAFILE; 
	rect:TRect;
	DocInfo: TDocInfo; 
	dc: HDC; 
	fHeader: PEnhMetaHeader;
	SzHdr,width,height,PageHeight,PageWidth,prWidth,prHeight,PxPerInchX,PxPerInchY,dpiX,dpiY:integer;
	Size : Integer;
begin 

hemf:=GetEnhMetaFile(PChar(emf));
if (hemf=0) then begin;msgok('Cannot read or support file '+emf+'!');Halt(1);end;
SzHdr := GetEnhMetaFileHeader( hemf, 0, nil );
fHeader := AllocMem( { SzHeader } Sizeof( fHeader^ ) );
fHeader.nSize := Sizeof( fHeader^ ) { SzHdr };
GetEnhMetaFileHeader( hemf, SzHdr, fHeader );
height:=fHeader.rclBounds.Bottom - fHeader.rclBounds.Top;
width:=fHeader.rclBounds.Right - fHeader.rclBounds.Left;
dpiX:=Round(fHeader.szlDevice.cx / fHeader.szlMillimeters.cx * 25.4);
dpiY:=Round(fHeader.szlDevice.cy / fHeader.szlMillimeters.cy * 25.4);

DC := CreateDC('WINSPOOL','Ps-Printer','FILE:',nil);
if DC=0 then begin;msgok('Cannot open printer Ps-Printer!');halt(3);end;
PageHeight:=GetDeviceCaps(DC, VertRes);
PageWidth:=GetDeviceCaps(DC, HorzRes);
PxPerInchX := GetDeviceCaps(DC, LOGPIXELSX); // pixels per inch
PxPerInchY := GetDeviceCaps(DC, LOGPIXELSY); // pixels per inch
prHeight := MulDiv (Height, PxPerInchY, dpiY);//96
prWidth :=  MulDiv (Width, PxPerInchX, dpiX);//96

rect.left := 0;
rect.top := PageHeight-prHeight;
rect.right := prWidth;
rect.bottom := PageHeight;

FillChar(DocInfo, sizeof(DocInfo), #0); 
DocInfo.cbSize := SizeOf(DocInfo); 
DocInfo.lpszDocName:='Generated by EMF2PS [mathmhb]';
DocInfo.lpszOutput:=PChar(f2);
StartDoc(dc, DocInfo); 
StartPage(dc); 

PlayEnhMetaFile (dc, hemf, rect);
EndPage(dc); 
EndDoc(dc); 
ReleaseDC(0,dc);

writeln('%%BoundingBox: 0 0 ',MulDiv(Width,72,dpiX):8,MulDiv(Height,72,dpiY):8);
writeln('%%HiBoundingBox: 0 0 ',Width*72.0/dpiX:8:2,Height*72.0/dpiY:8:2);
writeln('%%PageBoundingBox: 0 0 ',MulDiv(Width,72,dpiX):8,MulDiv(Height,72,dpiY):8);
writeln('%%Original File: [',emf,'] Size (pixels): [',width,'x',height,'] Resoulution (DPI): [',dpiX,'x',dpiY,']');


end;


begin;
  if ParamCount=0 then begin;msgok(usage);Halt(0);end;
  f1:=ParamStr(1);
  if ParamCount<2 then f2:=ChangeFileExt(f1,'.ps') else f2:=ParamStr(2);
  //writeln('Converting ',f1,'==)',f2);
  Emf2ps(f1,f2);
  if not FileExists(f2) then Halt(2);
  //writeln('Conversion finished!');
end.
